<template>
  <view class="goods_item">
    <view class="left">
      <radio @click="radioClick" v-if="showRadio" :checked="item.goods_status" color="#c00000"></radio>
      <image :src="item.goods_small_logo || defaultPic" mode="aspectFit"></image>
    </view>
    <view class="right">
      <view class="title">
        {{ item.goods_name }}
      </view>
      <view class="price">
        <view class="left">
          ￥{{ item.goods_price | tofixed }}
        </view>
        <uni-number-box @change="numChange" v-model="item.goods_count" v-if="showNum"></uni-number-box>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    data() {
      return {
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      };
    },
    props: {
      // 当前渲染的商品数据
      item: Object,
      // 控制单选按钮的显示与隐藏
      showRadio: {
        type: Boolean,
        default: false
      },
      // 控制 numberBox 的显示与隐藏
      showNum: {
        type: Boolean,
        default: false
      }
    },
    filters: {
      tofixed(num) {
        return num.toFixed(2)
      }
    },
    methods: {
      // 点击 numBox 的 + - 按钮时会触发
      numChange (e) {
        // 直接调用父组件中传入的事件
        this.$emit('num-change', {
          goods_id: this.item.goods_id,
          goods_count: e
        })
      },
      // 点击单选框会触发的事件
      radioClick() {
        this.$emit('radio-change', {
          // 商品 id
          goods_id: this.item.goods_id,
          // 商品点击后要修改的状态
          goods_status: !this.item.goods_status
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods_item {
    padding: 10px;
    display: flex;
    border-bottom: 1px solid #ccc;

    .left {
      display: flex;
      align-items: center;

      image {
        width: 120px;
        height: 120px;
        margin-right: 10px;
      }
    }

    .right {
      font-size: 14px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .price {
        color: #c00000;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
  }
</style>
